package com.sgxy.aclservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sgxy.aclservice.entity.AclEmp;
import com.sgxy.aclservice.entity.vo.AclEmpVo;
import com.sgxy.aclservice.mapper.AclEmpMapper;
import com.sgxy.aclservice.service.AclEmpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgxy.commonutils.JwtUtils;
import com.sgxy.commonutils.MD5;
import com.sgxy.servicebase.exceptionhandler.ERPException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 用户(员工) 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-26
 */
@Service
public class AclEmpServiceImpl extends ServiceImpl<AclEmpMapper, AclEmp> implements AclEmpService {
    //登录
    @Override
    public String login(AclEmp aclEmp) {
        String username = aclEmp.getUsername();
        String password = aclEmp.getPassword();
        //用户名密码判空
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            throw new ERPException(20001, "登录失败");
        }

        QueryWrapper<AclEmp> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        AclEmp emp = baseMapper.selectOne(wrapper);
        //数据库查询后数据判空
        if(StringUtils.isEmpty(emp)){
            throw new ERPException(20001, "登录失败");
        }
        //判断密码
        if(!MD5.encrypt(password).equals(emp.getPassword())) {
            throw new ERPException(20001, "登陆失败");
        }

        //判断用户是否禁用
        if(emp.getIsDeleted()){
            throw new ERPException(20001, "登录失败");
        }

        //登录成功，生成token
        String token = JwtUtils.getJwtToken(emp.getId(), emp.getNickName());
        return token;
    }
    //根据id获取角色
    @Override
    public AclEmpVo getEmpInfoById(String id) {

        return baseMapper.selectOneById(id);
    }

    @Override
    public IPage<AclEmpVo> selectPageVoList(IPage<AclEmpVo> page, AclEmp aclEmp) {
        return baseMapper.selectPageVoList(page, aclEmp);
    }


}
